<!--- Document Information -----------------------------------------------------

Title:      HibernateFactory.cfc

Author:     Mark Mandel
Email:      mark@compoundtheory.com

Website:    http://www.compoundtheory.com

Purpose:    Factory for getting to Hibernate objects

Usage:      

Modification Log:

Name			Date			Description
================================================================================
Mark Mandel		27/06/2006		Created

------------------------------------------------------------------------------->

<cfcomponent name="HibernateFactory" hint="Factory for getting to Hibernate objects">

<cfscript>
	instance = StructNew();
</cfscript>

<cffunction name="getHibernateFactory" access="private" returntype="any" output="false">
	<cfreturn variables.HibernateFactory />
</cffunction>

<cffunction name="setHibernateFactory" access="public" returntype="void" output="false">
	<cfargument name="HibernateFactory" type="any" required="true">
	<cfset variables.HibernateFactory = arguments.HibernateFactory />
</cffunction>

<cffunction name="getJarUtils" access="private" returntype="any" output="false">
	<cfreturn variables.JarUtils />
</cffunction>

<cffunction name="setJarUtils" access="public" returntype="void" output="false">
	<cfargument name="JarUtils" type="any" required="true">
	<cfset variables.JarUtils = arguments.JarUtils />
</cffunction>

<cffunction name="dumpvar">
  <cfargument name="var">
  <cfargument name="abort" default="true">
  <cftry>
	  <cfdump var="#var#">
	<cfcatch>
	  <cfset writeoutput(cfcatch.Message & " " & cfcatch.Detail & " " & cfcatch.TagContext[1].line & " " & cfcatch.stacktrace) />
	</cfcatch>
	</cftry>
	<cfif arguments.abort>
	  <cfabort />
	</cfif>
</cffunction>

<!------------------------------------------- PUBLIC ------------------------------------------->

<cffunction name="init" hint="Constructor: Loads the hibernate.cfg.xml from here, and any *.hbm.xml files that are in this directory" access="public" returntype="HibernateTools" output="false">
	<cfscript>
		return this;
	</cfscript>
</cffunction>

<cffunction name="generate" hint="Runs the Hibernate configure method" access="public" returntype="any" output="false">
	<cfargument name="hibernateConfigFile" required="true" />
	<cfargument name="reverseEngConfDirectory" required="true" />
	<cfargument name="hbmsLocation" required="true" />
	<cfargument name="sourcesDirectory" required="true" />
	<cfargument name="classesDirectory" required="true" />
	<cfargument name="jarFileNamePath" required="true" />
	<cfset var a = arguments />
	<cfset var genhbms = jdbc2hbm(a.hibernateConfigFile,a.reverseEngConfDirectory,a.hbmsLocation) />
	<cfset var jenjavas = hbm2java(a.hibernateConfigFile,a.hbmsLocation,a.sourcesDirectory) />
	<cfset var classes = compile(a.sourcesDirectory,'',a.classesDirectory) />
	<cfset var jar = createJarFile("/www/cfmappings/actionpacks/cfhibernate/tests/config/cfhibernatejar/cfhibernate/",
					jarFileNamePath) />
	
	<cfif classes["errors"] gt "">
	  <cfdump var="#classes.errors#"><cfabort />
	</cfif>
	<cfreturn classes />
</cffunction>


	<cffunction name="hbm2java" access="public" output="false" hint="creates java files from hbm files">
		<cfargument name="hibernateConfigFile" required="true">
		<cfargument name="hbmsLocation" required="true">
		<cfargument name="sourcesDirectory" required="true">
		<cfscript>
			var cnf = getHibernateFactory().newFile(arguments.hibernateConfigFile);
			var hbmsLoc = getHibernateFactory().newFile(arguments.hbmsLocation);
			var srcDir = getHibernateFactory().newFile(arguments.sourcesDirectory);
			var hibtooltask = getHibernateFactory().newHibernateToolTask();
			var config = hibtooltask.createConfiguration();
			var javaGen = "";
			try {
	 			config.setConfigurationFile(cnf);
	 			if(hbmsLoc.isDirectory()) {
					config.getConfiguration().addDirectory(hbmsLoc);
	 			} else {
					config.getConfiguration().addJar(hbmsLoc);
	 			}
				javaGen = hibtooltask.createHbm2Java();
				// set the output directory
				javaGen.setDestDir(srcDir);			
				javaGen.execute();
			}
			catch(Any excpt) {
				// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
			  dumpvar(excpt.message & " " & excpt.stacktrace);
			}
		</cfscript>	
	</cffunction>

	<cffunction name="jdbc2hbm" access="public" output="false" hint="creates hbm files from jdbc via configuration file">
		<!--- Sadly, it seems that you cannot set the jdbc connection by hand -- needs config files --->
		<cfargument name="configFile" hint="hibernate configuration file" />
		<cfargument name="reverseEngDir" hint="hibernate reverse engineering files (*.reveng.xml) directory" />
		<cfargument name="hbmExportDir" hint="target directory for hbm files" />
		<cfscript>
			var cnf = getHibernateFactory().newFile(arguments.configFile);
			var revEngDir = getHibernateFactory().newFile(arguments.reverseEngDir);
			var exportDir = getHibernateFactory().newFile(arguments.hbmExportDir);
			var fs = getHibernateFactory().newAntFileSet();
			var hibtooltask = getHibernateFactory().newHibernateToolTask();
			var javaGen = "";
			var hbmgen = "";
 			var proj = getHibernateFactory().newAntProject();
 			var revEng = getHibernateFactory().newAntPath(proj).init(proj);
			try {
	 			fs.setDir(revEngDir);
	 			fs.createInclude().setName( "**/*.reveng.xml" );
				javaGen = hibtooltask.createjdbcconfiguration();
	 			javaGen.setConfigurationFile(cnf);
	 			revEng.addFileset(fs);
				javaGen.setRevEngFile(revEng);
				// set the output directory
				hibtooltask.setDestDir(exportDir);
				hbmgen = hibtooltask.createHbm2HbmXml();
				hbmgen.execute();
			}
			catch(Any excpt) {
				// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
			  dumpvar(excpt.message & " " & excpt.tagcontext[1].line & " " & excpt.stacktrace);
			}
			return "success!";
		</cfscript>	
	</cffunction>

	<cffunction name="compile" access="public" output="false" hint="compiles java files">
		<cfargument name="srcdir">
		<cfargument name="classpath" default="">
		<cfargument name="outFileDir" default="#arguments.srcdir#">
		<cfscript>
			var compiler = getHibernateFactory().newCompiler();
			var didCompile = structNew();
			// get the system class path
			var system = getHibernateFactory().newJavaLangSystem();
			var filesep = system.getProperty("file.separator");
			var outStream = getHibernateFactory().newByteArrayOutputStream();
			var outS = getHibernateFactory().newByteArrayOutputStream();
			var errS = getHibernateFactory().newByteArrayOutputStream();
			var outWriter = getHibernateFactory().newPrintWriter(outS);
			var errWriter = getHibernateFactory().newPrintWriter(errS);
			// set the class deliminator for windows/linux
			switch(filesep){
				case "/":
					classdelim = ":";
				break;
				case "\":
					classdelim = ";";
				break;
			}
			if (arguments.classpath eq "") {
				syslibpath = replacenocase(system.getProperty("java.library.path"),'../lib','lib');
				//classpath = """" & system.getProperty("java.class.path") & classdelim & system.getProperty("java.library.path") & classdelim & arrayToList(variables._loader.queryJars(),classdelim) &"""" ;
				classpath = """" & system.getProperty("java.class.path") & classdelim & syslibpath &"""" ;
			}
			else {
				//classpath = """" & arguments.classpath & classdelim &  arrayToList(variables._loader.queryJars(),classdelim)  &"""" ;
				classpath = """" & arguments.classpath & classdelim;
			}
			sourcepath = """" & "/www/cfmappings/actionpacks/cfsvn/lib" &"""" ;
			didCompile["outFileDir"] = arguments.outFileDir;
			didCompile["command"] = "-classpath #classpath# #arguments.srcdir#";
			//didCompile["success"] = compiler.compile("-nowarn -sourcepath #sourcepath# -classpath #classpath# #arguments.javaFile# -d #arguments.outFileDir#",outWriter,errWriter);
			didCompile["success"] = compiler.compile("-nowarn -classpath #classpath# #arguments.srcdir# -d #arguments.outFileDir#",outWriter,errWriter);
			didCompile["errors"] = replace(errS.toString(),chr(13)&chr(10),"<br>","all");
			didCompile["out"] = outS.toString();
			outStream.close();
			outS.close();
			errS.close();
			outWriter.close();
			errWriter.close();
			return didCompile;
		</cfscript>
	</cffunction>

<cffunction name="createJarFile">
	<cfargument name="srcPath" />
	<cfargument name="destFile" default="#arguments.srcPath#/wee.jar" />
	<cfargument name="jarPrefix" default="" />
	<cfscript>
		var jarUtils = getJarUtils().createJarFile(srcPath,destFile,jarPrefix);
	</cfscript>
</cffunction>

<!------------------------------------------- PACKAGE ------------------------------------------->

<!------------------------------------------- PRIVATE ------------------------------------------->

</cfcomponent>